package cn.lijigou.shopping;

import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import cn.lijigou.shopping.alipay.Alipay;
import cn.lijigou.shopping.domain.MResult;
import cn.lijigou.shopping.domain.Order;
import cn.lijigou.shopping.service.MSystem;
import cn.lijigou.shopping.util.GetSingleThreadPool;
import cn.lijigou.shopping.util.Util;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Browser;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

/**
 * @author 赵奇 	cagezhao@gmail.com 
 * @version CreatTime：2011-4-13 下午04:14:46
 * 类说明
 */
public class OrderDetailActivity extends Activity implements OnClickListener {
	private ImageView table_image = null;
	private	TextView  table_title = null;
	private	TextView  source = null;
	private TextView back= null;
	private LinearLayout titleLeftView= null;
	private TextView register= null;
	private LinearLayout titleRightView= null;
	TextView text_center;
    Map<Integer,Bitmap> cacheBitMap=new HashMap<Integer,Bitmap>();
	private Bitmap bmImg  = null;
	private int cpsState=0;
	int x,lastX,init;
	int orderIndex=0;
	private TextView text_buy_time = null;
	private TextView text_price = null;
	private TextView text_pay_status = null;
	private TextView text_order_sn = null;
	private TextView text_expire_time = null;
	private TextView text_size = null;
	private RelativeLayout layout_info = null;
	private	TextView table_addressThree = null;
	private int NOTIFY_DATA_SET_CHANGED=101;
	private int NOTIFY_NO_DATA=102;
	public  static Dialog mDialog;
	public  Button mBankCard;
	public  Button mAlipay;
	public  LinearLayout layout_phone;
	public  ProgressBar mProgressBar;;
	public boolean isPaid;
	
	private Order order = new Order();
	private Dialog mConfirmDialog;
	private TextView CouponCodeError;
	private ProgressDialog mProgress = null;
	
	private ViewPager viewPager;
	private PageAdapter adapter;
	private LayoutInflater mInflater;
	private List<View> mListViews;
	private ExecutorService executorService = GetSingleThreadPool.getInstance();
	
	public Handler waitHander = new Handler(){
		
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.what ==0){
				Bitmap bitmap=cacheBitMap.get(orderIndex);
				if(bitmap!=null){
					table_image.setImageBitmap(bitmap);
					hideProgress();
				}
			}else if(msg.what ==1){
				mDialog.dismiss();
				hideProgress();
				Toast.makeText(OrderDetailActivity.this, R.string.cancel_success, 1).show();
				
				if(isPaid){
					OrdersActivity.mPayedOrders.remove(OrdersActivity.mPayedOrders.get(orderIndex));
				}else{
					OrdersActivity.mUnPayOrders.remove(OrdersActivity.mUnPayOrders.get(orderIndex));
				}
				
				
				OrderDetailActivity.this.finish();
			}else if(msg.what ==2){
				mDialog.dismiss();
				hideProgress();
				Toast.makeText(OrderDetailActivity.this, R.string.cancel_failed, 1).show();
			}else if(msg.what ==3){
				hideProgress();
				Intent intent = new Intent(OrderDetailActivity.this, OrderDetailShopActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				OrderDetailActivity.this.startActivity(intent);
				
			}else if(msg.what ==4){
				hideProgress();
				Toast.makeText(OrderDetailActivity.this, "主人，更新数据失败，请稍后再试", 1).show();
			}else if(msg.what == 5){
				
				Order order =  OrdersActivity.mUnPayOrders.get(orderIndex);
				Alipay alipay = new Alipay(OrderDetailActivity.this, order.getId(),order.getTuan_title());
				alipay.alipay();
				
			}else if(msg.what ==NOTIFY_DATA_SET_CHANGED){
				adapter.notifyDataSetChanged();
				hideProgress();
				if(text_center!=null){
					if(isPaid){
						text_center.setText("订单详情("+(orderIndex+1)+"/"+OrdersActivity.mPayedOrders.size()+")");
					}else{
						text_center.setText("订单详情("+(orderIndex+1)+"/"+OrdersActivity.mUnPayOrders.size()+")");
					}
					
				}
				
				
			}else if(msg.what == NOTIFY_NO_DATA){
				hideProgress();
			}

		}
	};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.order_detail);
		overridePendingTransition(R.anim.push_in,R.anim.push_out);
		
		Intent mIntent = this.getIntent();
		orderIndex = mIntent.getIntExtra("position", 0);
		
//		isPaid 已经支付的话是true
		isPaid= mIntent.getBooleanExtra("isPaid", false);
		
	 
		
		cacheBitMap.clear();
		setViews();
		ShopDetailActivity.mList.add(this);
		setOnClickListeners();
	}
	
	private void  setViews(){
		back = (TextView) this.findViewById(R.id.title_btn_left);
		titleLeftView = (LinearLayout) this.findViewById(R.id.titleLeftView);
		back.setBackgroundResource(R.drawable.title_back_normal);

		register = (TextView) this.findViewById(R.id.title_btn_right);
		register.setText("取消订单");
		titleRightView = (LinearLayout) this.findViewById(R.id.titleRightView);
		
		
		text_center = (TextView) this.findViewById(R.id.text_center);
		
		adapter = new PageAdapter();
		viewPager = (ViewPager) findViewById(R.id.viewpagerLayoutOrder);
		
		mListViews = new ArrayList<View>();
		mInflater = getLayoutInflater();

		for(int i=0;i<8;i++){
			View layout = mInflater.inflate(R.layout.scrollviews_order_detail, null);
			mListViews.add(layout);
		}
		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			public void onPageSelected(int position) {
				
				if(isPaid){
					if (position < OrdersActivity.mPayedOrders.size()) {
						orderIndex = position;
						if (position == OrdersActivity.mPayedOrders.size() - 1) {
							getOrders();
						}
						setDate(OrdersActivity.mPayedOrders.get(position));
					}
				}else{
					if (position < OrdersActivity.mUnPayOrders.size()) {
						orderIndex = position;
						if (position == OrdersActivity.mUnPayOrders.size() - 1) {
							getOrders();
						}
						setDate(OrdersActivity.mUnPayOrders.get(position));
					}
				}
				
			}

			
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		if(isPaid){
			setDate(OrdersActivity.mPayedOrders.get(orderIndex));
		}else{
			setDate(OrdersActivity.mUnPayOrders.get(orderIndex));
		}
		viewPager.setCurrentItem(orderIndex);
		adapter.notifyDataSetChanged();
		
	}
	private void  setOnClickListeners(){
		titleLeftView.setOnClickListener(this);
		layout_info.setOnClickListener(this);
		titleRightView.setOnClickListener(this);
		mBankCard.setOnClickListener(this);
		mAlipay.setOnClickListener(this);
		layout_phone.setOnClickListener(this);
		
	}
	
	public void checkPayStatus(View v){
		
		order =  OrdersActivity.mUnPayOrders.get(orderIndex);
		cpsState = order.getCpsState();
		String code_sn = order.getCode_sn();
		String code_status = order.getCode_status();
		String code_money = order.getCode_money();
		String code_errmsg = order.getCode_errmsg();
		
		if(!"".equals(code_sn)){
			//优惠码 状态 1-->合法，可以使用 0--> 已过期
			if(code_status.equals("1")){
				confirmCpsPayDialog(order,v);
			}else if(code_status.equals("0")){
				LayoutInflater factory=LayoutInflater.from(OrderDetailActivity.this);
				final View v1=factory.inflate(R.layout.confirm_pay_coupon_code,null);
				CouponCodeError = (TextView) v1.findViewById(R.id.tv_coupon_code);
				CouponCodeError.setText(code_errmsg);
				
				
				mConfirmDialog = new Dialog(OrderDetailActivity.this,android.R.style.Theme_Translucent_NoTitleBar);
				mConfirmDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
		                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
				mConfirmDialog.setContentView(v1);
		 
				
				((Button) mConfirmDialog.findViewById(R.id.ok))
						.setOnClickListener(new OnClickListener() {

							
							public void onClick(View v) {
								
								confirmCpsPayDialog(order,v);
								mConfirmDialog.dismiss();
							}
						});
				((Button) mConfirmDialog.findViewById(R.id.cancel))
						.setOnClickListener(new OnClickListener() {

							
							public void onClick(View v) {
								mConfirmDialog.dismiss();
							}
						});
				setShadow(true);
				mConfirmDialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
				mConfirmDialog.show();
				
				mConfirmDialog.setOnDismissListener(new OnDismissListener() {
					
					public void onDismiss(DialogInterface dialog) {
						setShadow(false);
					}
				});
			}
		}else{
			confirmCpsPayDialog(order,v);
		}
	}
	
	
	
	private void confirmCpsPayDialog(Order order,final View v) {
		int start = order.getBuy_start_time();
		int end = order.getBuy_end_time();
		String isBuy = order.getIs_buy();
		if (cpsState > 0) {
			boolean isCps = MSystem.isCps(order.getTuan_id()).isSuccess();
			if (isCps) {
				showPayDialog(v);
			}else{
				Toast.makeText(OrderDetailActivity.this, R.string.sorry_tips3, 1).show();
			}
			
		}else{
			if(isBuy != null){
				if( isBuy.equals("1") || isBuy.equals("2")){
					
					Integer hour = Util.getCurSysTime();
					if(hour >= start && hour < end){
						showPayDialog(v);
					}else{
						new AlertDialog.Builder(OrderDetailActivity.this)
		                .setIcon(R.drawable.icon)
		                .setTitle("是否继续购买？")
		                .setMessage(end+":00-"+start+":00间的订单，我们将在第二天工作时间帮您处理")
		                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int whichButton) {
		                    	showPayDialog(v);
		                    }
		                })
		                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int whichButton) {
		                    	dialog.dismiss();
		                    }
		                })
		                .create().show();
					}
				}else{
					Toast.makeText(this, "亲，该商品已过期，不能继续购买,请查看其他同类商品", 1).show();
				}
			}else{
				Toast.makeText(this, "亲，该商品已过期，不能继续购买,请查看其他同类商品", 1).show();
			}
			
			
		}
	}
	
	public void setShadow(Boolean isTransparent){
		WindowManager.LayoutParams lp=getWindow().getAttributes();
		if(isTransparent){
	        lp.alpha=0.3f;
	        lp.dimAmount=0.3f;
	        getWindow().setAttributes(lp);
	        getWindow().setAttributes(lp);
		}else{
			lp.alpha=1.0f;
	        lp.dimAmount=1.0f;
	        getWindow().setAttributes(lp);
	        getWindow().setAttributes(lp);
		}

	}
	
	private void showPayDialog(View v) {
		
		
		if(v.getId() == R.id.alipay){
			Order order =  OrdersActivity.mUnPayOrders.get(orderIndex);
			Alipay alipay = new Alipay(OrderDetailActivity.this, order.getId(),order.getTuan_title());
			alipay.alipay();
			
		}else if(v.getId() == R.id.bank_card){
			Alipay alipay_bank=null;
			Order order =  OrdersActivity.mUnPayOrders.get(orderIndex);
			alipay_bank  = new Alipay(OrderDetailActivity.this, order.getId(),order.getTuan_title());
			alipay_bank.alipayCard();
		}
	}
	
	private void showCanclDialog() {
		LayoutInflater factory=LayoutInflater.from(OrderDetailActivity.this);
		final View v1=factory.inflate(R.layout.cancle_order,null);
		mDialog = new Dialog(OrderDetailActivity.this,android.R.style.Theme_Translucent_NoTitleBar);
		mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		mDialog.setContentView(v1);
 
		((Button) mDialog.findViewById(R.id.ok))
				.setOnClickListener(new OnClickListener() {

					
			public void onClick(View v) {
				// write your code to do things after users clicks CANCEL
				showProgress();
				executorService.execute(new Thread() {
					public void run() {
						Boolean getOrder;

						getOrder = MSystem.cancel_order(OrdersActivity.mUnPayOrders.get(orderIndex).getId()).isSuccess();
						if (getOrder != null) {
							if (getOrder) {
								waitHander.sendEmptyMessage(1);
							} else {
								waitHander.sendEmptyMessage(2);
							}
						} else {
							waitHander.sendEmptyMessage(2);
						}

					}
				});

			}
				});
		((Button) mDialog.findViewById(R.id.cancel))
				.setOnClickListener(new OnClickListener() {

					
					public void onClick(View v) {
						// write your code to do things after users clicks OK
						//在订单详情--商品详情下单时改变Orders.isRefresh的值为true，实现去网络端重新更新订单
						mDialog.dismiss();
					}
				});
		setShadow(true);
		mDialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
		mDialog.show();
		
		mDialog.setOnDismissListener(new OnDismissListener() {
			
			public void onDismiss(DialogInterface dialog) {
				// TODO Auto-generated method stub
				setShadow(false);
			}
		});
	}
	
	public void onClick(View v) {
		MSystem.pv += 1;
		switch (v.getId()) {
		case R.id.titleLeftView:
			if (ShopDetailActivity.mList.contains(this)) {
				ShopDetailActivity.mList.remove(this);
			}
			Intent intent_s = new Intent(OrderDetailActivity.this, OrdersActivity.class);
			OrderDetailActivity.this.setResult(1, intent_s);
			this.finish();
			break;
		case R.id.layout_infoThree: // 团购详情
			MSystem.detail +=1;
			Intent intentthree = new Intent(OrderDetailActivity.this, OrderDetailShopActivity.class);

			intentthree.putExtra("isPaid", isPaid);
			intentthree.putExtra("position", orderIndex);
			OrderDetailActivity.this.startActivity(intentthree);
			break;
		case R.id.titleRightView: // 取消订单
			showCanclDialog();
			break;
		case R.id.bank_card:
			MSystem.buy += 1;
			checkPayStatus(v);
			break;
		case R.id.alipay:
			MSystem.buy += 1;
			checkPayStatus(v);
			break;
		case R.id.layout_phone:
			call();
			break;
		}
	}
	
	public void call() {
			Uri uri = Uri.parse("tel:"+"400-705-5700");
		    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		    intent.putExtra(Browser.EXTRA_APPLICATION_ID, this.getPackageName());
		    this.startActivity(intent);
	}
	
	public void setDate(final Order order){
		View parent = mListViews.get(orderIndex%8);
		
		table_image  = (ImageView) parent.findViewById(R.id.table_imageThree);
		table_title  = (TextView) parent.findViewById(R.id.table_titleThree);
		source  = (TextView) parent.findViewById(R.id.source);
		text_buy_time  = (TextView) parent.findViewById(R.id.text_buy_timeThree);
		text_price  = (TextView) parent.findViewById(R.id.text_priceThree);
		text_size  = (TextView) parent.findViewById(R.id.text_sizeThree);
		text_pay_status  = (TextView) parent.findViewById(R.id.text_pay_statusThree);
		text_order_sn  = (TextView) parent.findViewById(R.id.text_order_snThree);
		text_expire_time  = (TextView) parent.findViewById(R.id.text_expire_timeThree);
		layout_info  = (RelativeLayout) parent.findViewById(R.id.layout_infoThree);
		table_addressThree = (TextView) parent.findViewById(R.id.table_addressThree);
		mBankCard = (Button) parent.findViewById(R.id.bank_card);
		mAlipay = (Button) parent.findViewById(R.id.alipay);
		layout_phone = (LinearLayout) parent.findViewById(R.id.layout_phone);
		
		
		setOnClickListeners();
		if(isNull(order.getAddress())){
			table_addressThree.setText(order.getAddress());
		}else{
			table_addressThree.setVisibility(View.GONE);
		}
		
		if("1".equals(order.getPay_status())){
				mBankCard.setVisibility(View.GONE);
				mAlipay.setVisibility(View.GONE);
				titleRightView.setVisibility(View.GONE);
		}else{
				mBankCard.setVisibility(View.VISIBLE);
				mAlipay.setVisibility(View.VISIBLE);
				titleRightView.setVisibility(View.VISIBLE);
		}
		
		
		
		
		if(isNull(order.getImage())){
			String  filePath = "/sdcard/tuangou/"+order.getTuan_id()+"small.jpg";
			File file=new File(filePath);
			if(file.exists()){
				Bitmap bitmap=BitmapFactory.decodeFile(filePath);
				if(bitmap!=null){
					 
					cacheBitMap.put(orderIndex,bitmap);
					waitHander.sendEmptyMessage(0);
				}
			}else{
				Thread thread = new Thread(new Runnable() {
					public void run() {
						if(cacheBitMap.get(orderIndex)==null){
							cacheBitMap.put(orderIndex, downloadImage(order.getImage()));
						}
						waitHander.sendEmptyMessage(0);
					}
				});
				thread.start();
			}
		}
		
		
		
		if(isNull(order.getTuan_title())){
				table_title.setText(order.getTuan_title().trim()+"...");
		}else{
			table_title.setText("暂无详情");
		}
		if(isNull(order.getTuan_site())){
			source.setText(order.getTuan_site());
		}else{
			source.setText("未知来源");
		}
		if(isPaid){
			if(OrdersActivity.mPayedOrders.size()>0){
				text_center.setText("订单详情("+(orderIndex+1)+"/"+OrdersActivity.mPayedOrders.size()+")");
			}
		}else{
			if(OrdersActivity.mUnPayOrders.size()>0){
			text_center.setText("订单详情("+(orderIndex+1)+"/"+OrdersActivity.mUnPayOrders.size()+")");
			}
		}
		
		if(isNull(order.getBuy_time())){
				text_buy_time.setText(getResources().getString(R.string.buy_date) + order.getBuy_time());
		}
		if(isNull(order.getQuantity())){
				text_size.setText(getResources().getString(R.string.count) + order.getQuantity());
		}
		if(isNull(order.getTotal_price())){
				text_price.setText(getResources().getString(R.string.total_price) + order.getTotal_price());
		}
		if(isNull(order.getPay_status())){
			if("0".equals(order.getPay_status())){
					text_pay_status.setText(R.string.order_status_not_paid);
			}else if("1".equals(order.getPay_status())){
					text_pay_status.setText(R.string.order_status_success_pay);
			}else if("2".equals(order.getPay_status())){
					text_pay_status.setText(R.string.order_status_failed_to_pay);
			}
		}
		
		if(isNull(order.getOrder_sn())){
				text_order_sn.setText( order.getOrder_sn());
		}
		if(isNull(order.getExpire_time())){
				text_expire_time.setText(getResources().getString(R.string.expiration_date) + order.getExpire_time());
		}
		
		if("0".equals(order.getTotal_price()) || "0.0".equals(order.getTotal_price()) || "0.00".equals(order.getTotal_price())){
			text_pay_status.setText("0元不支持再次支付，请重新下单");
			mBankCard.setVisibility(View.GONE);
			mAlipay.setVisibility(View.GONE);
		}
	}
	
	private Bitmap downloadImage(String urlstr){ 
		  InputStream is = null;
      try { 
    	  	URL url= new URL(urlstr); 
    	  	HttpURLConnection conn= (HttpURLConnection)url.openConnection(); 
           	conn.setConnectTimeout(5*1000);
			  	conn.setDoInput(true);
				conn.setDoOutput(true);
			
			if(conn.getResponseCode()==200){
				is = conn.getInputStream(); 
				bmImg = BitmapFactory.decodeStream(is); 
				is.close();
			}
      } catch (Exception e) {
           e.printStackTrace(); 
      }
      return bmImg;
	} 
	
	public Boolean isNull(String str){
		if(str!=null&&!str.equals("")&&str!=""){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 显示等待框
	 * 
	 * @param title
	 * @param message
	 */
	protected void showProgress() {
		this.runOnUiThread(new Runnable() {
			public void run() {	
				mProgress = new ProgressDialog(OrderDetailActivity.this);
				mProgress.requestWindowFeature(Window.FEATURE_NO_TITLE);
				mProgress.setMessage(OrderDetailActivity.this.getResources().getString(R.string.loading_please_wait));
				mProgress.show();
			}
		});
	}

	/**
	 * 取消等待框
	 */
	protected void hideProgress() {
		
		this.runOnUiThread(new Runnable() {
			public void run() {
				if (mProgress != null) {
					mProgress.dismiss();
				}
			}
		});
	}



	private void getOrders() {
		if(getNetConnectState(OrderDetailActivity.this)){
			
			 if(isPaid){
				if (OrdersActivity.hasNextPages[1]) {
					OrdersActivity.hasNextPages[1] = false;
					showProgress();
					executorService.execute(new Thread() {
						public void run() {
							Boolean temp = false;
							OrdersActivity.pages[1] = OrdersActivity.pages[1] + 1;
							MResult mResult = MSystem.getOrders(OrdersActivity.mPayedOrders, OrdersActivity.pages[1], 1);
							if (mResult != null) {
								if (mResult.isSuccess()) {
									temp = mResult.isPage();
									waitHander.sendEmptyMessage(NOTIFY_DATA_SET_CHANGED);
									if (!temp) {
										waitHander.sendEmptyMessage(NOTIFY_NO_DATA);
										MainTabActivity.showHander.sendEmptyMessage(102);
									}

								} else {
									if (OrdersActivity.mPayedOrders.size() > 0) {
										MainTabActivity.showHander.sendEmptyMessage(102);
									}

									waitHander.sendEmptyMessage(NOTIFY_NO_DATA);

								}
							} else {
								if (OrdersActivity.mPayedOrders.size() == 0) {
									OrdersActivity.pages[1] = 1;
								} else {
									OrdersActivity.pages[1] = OrdersActivity.pages[1] - 1;
								}
								waitHander.sendEmptyMessage(3);
								temp = true;
							}
							OrdersActivity.hasNextPages[1] = temp;
						}
					});

				}
			} else {

				OrdersActivity.hasNextPages[0] = false;
				showProgress();
				executorService.execute(new Thread() {
					public void run() {
						Boolean temp = false;
						OrdersActivity.pages[0] = OrdersActivity.pages[0] + 1;
						MResult mResult = MSystem.getOrders(OrdersActivity.mUnPayOrders, OrdersActivity.pages[0], 0);
						if (mResult != null) {
							if (mResult.isSuccess()) {
								temp = mResult.isPage();
								waitHander.sendEmptyMessage(NOTIFY_DATA_SET_CHANGED);
								if (!temp) {
									waitHander.sendEmptyMessage(NOTIFY_NO_DATA);
									MainTabActivity.showHander.sendEmptyMessage(102);
								}

							} else {
								if (OrdersActivity.mPayedOrders.size() > 0) {
									MainTabActivity.showHander.sendEmptyMessage(102);
								}

								waitHander.sendEmptyMessage(NOTIFY_NO_DATA);

							}
						} else {
							if (OrdersActivity.mPayedOrders.size() == 0) {
								OrdersActivity.pages[0] = 1;
							} else {
								OrdersActivity.pages[0] = OrdersActivity.pages[0] - 1;
							}
							waitHander.sendEmptyMessage(3);
							temp = true;
						}
						OrdersActivity.hasNextPages[0] = temp;
					}
				});

			}
		}
	}
	
	

	public boolean getNetConnectState(Context context) {
		boolean netConnect = false;
		ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo infoM = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (info.isConnected() || infoM.isConnected()) {
			netConnect = true;
		}
		return netConnect;
	}
	
	
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(ShopDetailActivity.mList.contains(this)){
				ShopDetailActivity.mList.remove(this);
			}
			this.finish();
			return true;
		}
		return false;
	}
	
	private class PageAdapter extends PagerAdapter {
		// 销毁arg1位置的界面
		
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1 % 8));
		}

		// 获取当前窗体界面数
		
		public int getCount() {
			
			if(isPaid){
				return OrdersActivity.mPayedOrders.size();
			}else{
				return OrdersActivity.mUnPayOrders.size();
			}
		}

		// 初始化arg0位置的界面
		
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1 % 8), 0);
			return mListViews.get(arg1 % 8);
		}

		// 判断是否由对象生成界面
		
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}
	}
}
